package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.sc.nft.dao.MallRankingListPoolDao;
import com.sc.nft.entity.MallRankingListPool;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.MallRankingListPoolService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 商城排行榜总奖池 服务实现类
 * </p>
 *
 * @author ligen
 * @since 2024-01-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallRankingListPoolServiceImpl implements MallRankingListPoolService {

    private final MallRankingListPoolDao mallRankingListPoolDao;

    @Override
    public MallRankingListPool getResidualPrizePool() {
        return mallRankingListPoolDao.getResidualPrizePool();
    }

    @Override
    public Boolean subAmount(BigDecimal initPoolReserve) {
        MallRankingListPool rankingListPool = getResidualPrizePool();
        Assert.isTrue(NumberUtil.isGreaterOrEqual(rankingListPool.getAmount(), initPoolReserve), () -> new GlobalRunTimeException("可用余额不足"));
        return mallRankingListPoolDao.subAmount(initPoolReserve,rankingListPool.getId());
    }

    @Override
    public Boolean addAmount(BigDecimal initPoolReserve) {
        MallRankingListPool rankingListPool = getResidualPrizePool();
        return mallRankingListPoolDao.addAmount(initPoolReserve,rankingListPool.getId());
    }

    @Override
    public void subFreezeAmount(BigDecimal subFreezeAmount) {
        MallRankingListPool rankingListPool = getResidualPrizePool();
        mallRankingListPoolDao.subFreezeAmount(subFreezeAmount,rankingListPool.getId());
    }
}
